//
//  RowControlsController.m
//  Nav
//
//  Created by KaiKai on 11-8-7.
//  Copyright 2011年 SDS. All rights reserved.
//

#import "RowControlsController.h"

@implementation RowControlsController

@synthesize list;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)dealloc
{
	[list release];
	
	[super dealloc];
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
	
	NSArray *array = [[NSArray alloc] initWithObjects:@"你是好人吗？", @"昨天下雨了吗？", @"“梅超风”厉害吗？", @"你想出国吗？", @"想中彩票吗？", nil];
	self.list = array;
	[array release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *ControlRowIdentifier = @"ControlRowIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ControlRowIdentifier];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ControlRowIdentifier] autorelease];
		UISwitch *switchView = [[UISwitch alloc] init];
		switchView.tag = KSwitchTag;
		cell.accessoryView = switchView;
		[switchView release];
	}
	NSUInteger row = [indexPath row];
	NSString *rowTitle = [list objectAtIndex:row];
	cell.text = rowTitle;

	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row = [indexPath row];
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	UISwitch *switchView = (UISwitch *)[cell viewWithTag:KSwitchTag];
	
	NSString *baseString = @"%@ %@.";
	NSString *onString = (switchView.on) ? @"\nAnswer is On" : @"\nAnswer is not On";
	NSString *robot = [list objectAtIndex:row];
	NSString *messageString = [[NSString alloc] initWithFormat:baseString, robot, onString];
	
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected." message:messageString delegate:nil cancelButtonTitle:@"Thanks for your answer!" otherButtonTitles:nil];
	[alert show];
	[alert release];
	[messageString release];
}

@end
